import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";
import researchTeamRouter from "./modules/researchTeamRouter";
import machineManagementRouter from "./modules/machineManagementRouter";
import userSettingsRouter from "./modules/userSettingsRouter";
import projectManagementRouter from "./modules/projectManagementRouter";
import myResearchRouter from "./modules/myResearchRouter";

/**
 * Note: sub-menu only appear when route children.length >= 1
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/auth-redirect",
    component: () => import("@/views/login/auth-redirect"),
    hidden: true,
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index"),
        name: "主页",
        meta: { title: "主页", icon: "dashboard", affix: true },
      },
    ],
  },
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: "/announcement",
    component: Layout,
    redirect: "/announcement/list",
    children: [
      {
        path: "create",
        component: () => import("@/views/announcement/Create"),
        name: "CreateAnnouncement",
        hidden: true,
        meta: {
          title: "新增通知",
          permission: "通知公告：创建",
        },
      },
      {
        path: "edit/:id(\\d+)",
        component: () => import("@/views/announcement/Edit"),
        name: "EditAnnouncement",
        meta: {
          title: "编辑通知",
          permission: "通知公告：更新",
          noCache: true,
          activeMenu: "/announcement/list",
        },
        hidden: true,
      },
      {
        path: "view/:id(\\d+)",
        component: () => import("@/views/announcement/View"),
        name: "ViewAnnouncement",
        meta: {
          title: "查看通知",
          permission: "通知公告：浏览",
          noCache: true,
          activeMenu: "/announcement/view",
        },
        hidden: true,
      },
      {
        path: "list",
        component: () => import("@/views/announcement/List"),
        name: "AnnouncementList",
        meta: {
          title: "通知公告",
          permission: "通知公告：浏览",
          icon: "message",
        },
      },
      {
        path: "/notification/list",
        component: () => import("@/views/notification/List"),
        name: "MyNotificationList",
        hidden: true,
        meta: {
          title: "项目提醒",
          permission: "通知公告：浏览",
        },
      },
    ],
  },
  // researchTeamRouter,
  projectManagementRouter,
  {
    path: "/researchgroup",
    component: Layout,
    redirect: "/researchgroup/list",
    children: [
      {
        path: "create",
        component: () => import("@/views/research-group/Create"),
        name: "CreateResearchGroup",
        hidden: true,
        meta: {
          title: "新增课题组",
          permission: "课题组信息：创建",
        },
      },
      {
        path: "edit/:id(\\d+)",
        component: () => import("@/views/research-group/Edit"),
        name: "EditResearchGroup",
        meta: {
          title: "编辑课题组",
          permission: "课题组信息：更新",
          noCache: true,
          activeMenu: "/researchgroup/list",
        },
        hidden: true,
      },
      {
        path: "view/:id(\\d+)",
        component: () => import("@/views/research-group/View"),
        name: "ViewResearchGroup",
        meta: {
          title: "查看课题组",
          permission: "课题组信息：浏览",
          noCache: true,
          activeMenu: "/researchgroup/view",
        },
        hidden: true,
      },
      {
        path: "list",
        component: () => import("@/views/research-group/List"),
        name: "ResearchGroupList",
        meta: {
          title: "课题组管理",
          permission: "课题组信息：浏览",
          icon: "table",
        },
      },
    ],
  },
  // {
  //   path: "/appropriation",
  //   component: Layout,
  //   redirect: "/appropriation/list",
  //   children: [
  //     {
  //       path: "create",
  //       component: () => import("@/views/appropriation/Create"),
  //       name: "CreateAppropriation",
  //       hidden: true,
  //       meta: { title: "新增经费科目",
  //         permission: "经费预算科目：创建",
  //       },
  //     },
  //     {
  //       path: "edit/:id(\\d+)",
  //       component: () => import("@/views/appropriation/Edit"),
  //       name: "EditAppropriation",
  //       meta: {
  //         title: "编辑经费科目信息",
  //         permission: "经费预算科目：更新",
  //         noCache: true,
  //         activeMenu: "/appropriation/list",
  //       },
  //       hidden: true,
  //     },
  //     {
  //       path: "approve/process/:id(\\d+)",
  //       component: () => import("@/views/appropriation/ProcessApprove"),
  //       name: "ProcessApprove",
  //       meta: {
  //         title: "经费科目流程审批",
  //         permission: "经费预算科目：审批",
  //         noCache: true,
  //         activeMenu: "/appropriation/processapprove",
  //       },
  //       hidden: true,
  //     },
  //     {
  //       path: "approve/cost/:id(\\d+)",
  //       component: () => import("@/views/appropriation/CostApprove"),
  //       name: "CostApprove",
  //       meta: {
  //         title: "经费科目财务审批",
  //         permission: "经费预算科目：财务审批",
  //         noCache: true,
  //         activeMenu: "/appropriation/costapprove",
  //       },
  //       hidden: true,
  //     },
  //     {
  //       path: "view/:id(\\d+)",
  //       component: () => import("@/views/appropriation/View"),
  //       name: "ViewAppropriation",
  //       meta: {
  //         title: "查看经费科目信息",
  //         permission: "经费预算科目：浏览",
  //         noCache: true,
  //         activeMenu: "/appropriation/view",
  //       },
  //       hidden: true,
  //     },
  //     {
  //       path: "list",
  //       component: () => import("@/views/appropriation/List"),
  //       name: "AppropriationList",
  //       meta: {
  //         title: "经费科目管理",
  //         permission: "经费预算科目：浏览",
  //         icon: "money",
  //       },
  //     },
  //   ],
  // },
  machineManagementRouter,
  myResearchRouter,
  userSettingsRouter,
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
